package com.kaiwukj.android.ufamily.mvp.ui.page.account.login.forget;

import android.os.Bundle;
import android.text.InputType;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.RegexUtils;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseFragment;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.mvp.http.entity.event.ForgetPassEvent;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

@Route(path = ARouterUrlKt.ForgetPassFragmentUrl)
public class ForgetPassFragment extends BaseFragment {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_input)
    EditText etInput;
    @BindView(R.id.btn_next)
    Button btnNext;

    @Autowired
    int pos = 0;
    @Autowired
    int type = 0;//normal:0  wechat:1

    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.fragment_login_forget;
    }

    @Override
    protected void initView() {
        etInput.requestFocus();
        if (pos == 1) {
            tvTitle.setText("确认新密码");
            etInput.setHint("请输入新密码");
            etInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            btnNext.setOnClickListener(v -> {
                String pass = etInput.getText().toString().trim();
                if (pass.length() >= 6 && RegexUtils.isMatch("^[0-9a-zA-Z]+$", pass)) {
                    EventBus.getDefault().post(new ForgetPassEvent(2, pass));
                } else {
                    showMessage("请输入至少6位字符，只能是字母或数字");
                }
            });
        } else {
            if (type != 0) {
                tvTitle.setText("绑定手机号");
            }
            btnNext.setOnClickListener(v -> {
                // start(SendVerifyCodeFragment.newInstance("15949581550"));
                String mobile = etInput.getText().toString().trim();
                if (!RegexUtils.isMobileSimple(mobile)) {
                    showMessage("请输入正确的手机号码!");
                    return;
                }
                EventBus.getDefault().post(new ForgetPassEvent(1, mobile));
            });
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }
}
